<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 19-7-31
 * Time: 下午5:06
 */

namespace common\components\gps;


use GuzzleHttp\Client;
use wodrow\yii\rest\ApiException;

class BeiDou1 extends Gps implements Api
{
    const PRE_SIGN = '863'; // 863014533077868 14144592206
    const NOTE = '北斗'; // http://fk.gpsonline.cn/fw/getView?fwCoord.project=&fwCoord.function=login&fwCoord.view=login

    /**
     * @throws
     */
    public function addGps()
    {
        $this->checkOrder();
        $order = $this->order;
        $_data = unserialize($order->data);
        /*$_x = [
            'client_name' => 'test2',
            'vehicleNum' => '浙DX911U',
        ];
        echo serialize($_x);exit;*/
        if (!isset($_data['client_name'])){
            throw new ApiException(201907311708, "子组织名必填");
        }
        if (!$_data['custom_name']){
            throw new ApiException(201907311709, "没有找到客户名");
        }
        $raw_arr = [
            'consumer' => [
                'name' => $_data['custom_name'],
            ],
            'observeObj' => [
                'deviceIds' => [$order->gps_serial_number],
                'observeGrp' => [
                    'org' => [
                        'name' => $_data['client_name'],
                    ],
                ],
            ],
        ];
        $json = json_encode($raw_arr, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => Config::$bei_dou1['sync_url'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
                'Accept-Encoding' => 'utf-8',
                'Accept' => 'application/json',
            ],
            'query' => [
                'token' => Config::$bei_dou1['token'],
            ],
        ]);
        $contents = $resp->getBody()->getContents();
        $c_arr = json_decode($contents, true);
        if (!isset($c_arr['rslt']['isSuc'])){
            throw new ApiException(201908011011, "绑定异常");
        }
        if ($c_arr['rslt']['isSuc'] != true){
            throw new ApiException(201908011012, $c_arr['rslt']['data']);
        }
    }

    public function getGps()
    {
        $this->checkOrder();
        $order = $this->order;
        $raw_arr = [
            'id' => $order->gps_serial_number,
        ];
        $json = json_encode($raw_arr, JSON_UNESCAPED_UNICODE);
        $client = new Client([
            'base_uri' => Config::$bei_dou1['get_gps_url'],
        ]);
        $resp = $client->request('POST', '', [
            'body' => $json,
            'headers' => [
                'Content-Type' => 'application/json',
                'Accept-Encoding' => 'utf-8',
                'Accept' => 'application/json',
            ],
            'query' => [
                'token' => Config::$bei_dou1['token'],
            ],
        ]);
        $contents = $resp->getBody()->getContents();
        $c_arr = json_decode($contents, true);
        if (!isset($c_arr['rslt']['isSuc'])){
            throw new ApiException(201908011013, "获取异常");
        }
        if ($c_arr['rslt']['isSuc'] != true){
            throw new ApiException(201908011014, $c_arr['rslt']['data']);
        }
        $other_data['gps_status'] = $c_arr['rslt']['data']['deviceStat']['online']?"已上线":"未上线";
        return [
            'platform_back' => $contents,
            'order' => $order->toArray(),
            'other_data' => $other_data,
        ];
    }

    public function getGpsLocationData()
    {
        $this->checkOrder();
        $order = $this->order;
        $data['url'] = Config::$bei_dou1['get_gps_location_url']."?id=".$order->gps_serial_number."&token=".Config::$bei_dou1['token'];
        $data['html'] = '<script type="text/javascript">location.href = "'.$data['url'].'";</script>';
        return $data;
    }
}